package gui;

import ge.escola.Serie;
import ge.escola.Turma;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

import javax.swing.WindowConstants;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class CadastraTurmaPanel extends javax.swing.JPanel {
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JTextField caixaNome;
	Color verde = new Color(0,150,0);
	private String nomeTurma,nomeSerie;
	private Serie serie;
	private JTextField caixaSerie;
	private JButton buttonCancelar;
	private JButton buttonLimpar;
	private JButton buttonCadastrar;
	private JLabel jLabel5;
	private JLabel jLabel4;
	private JLabel checagemSerie;
	private JLabel checagemTurma;
	
	boolean flag = true;
	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	
	public CadastraTurmaPanel() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setPreferredSize(new Dimension(400, 300));
			this.setSize(800, 600);
			this.setLayout(null);
			this.setBackground(new java.awt.Color(204,204,255));
			{
				jLabel1 = new JLabel();
				this.add(jLabel1);
				jLabel1.setText("Cadastra turma");
				jLabel1.setFont(new java.awt.Font("Tahoma",0,48));
				jLabel1.setBounds(237, 5, 325, 58);
				jLabel1.setForeground(new java.awt.Color(0,102,102));
			}
			{
				jLabel2 = new JLabel();
				this.add(jLabel2);
				jLabel2.setText("Nome da turma");
				jLabel2.setBounds(66, 184, 168, 33);
				jLabel2.setFont(new java.awt.Font("Tahoma",0,24));
				jLabel2.setForeground(new java.awt.Color(0,102,102));
			}
			{
				jLabel3 = new JLabel();
				this.add(jLabel3);
				jLabel3.setText("Nome da serie");
				jLabel3.setBounds(66, 274, 157, 34);
				jLabel3.setFont(new java.awt.Font("Tahoma",0,24));
				jLabel3.setForeground(new java.awt.Color(0,102,102));
			}
			{
				caixaNome = new JTextField();
				this.add(caixaNome);
				this.add(getCaixaSerie());
				this.add(getChecagemTurma());
				this.add(getJLabel4());
				this.add(getJLabel4x());
				this.add(getJLabel5());
				this.add(getButtonCadastrar());
				this.add(getJButton1());
				this.add(getJButton2());
				caixaNome.setBounds(252, 187, 72, 33);
				caixaNome.setFont(new java.awt.Font("Tahoma",0,18));
				caixaNome.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						caixaNomeKeyReleased(evt);
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private JTextField getCaixaSerie() {
		if(caixaSerie == null) {
			caixaSerie = new JTextField();
			caixaSerie.setBounds(252, 280, 72, 30);
			caixaSerie.setFont(new java.awt.Font("Tahoma",0,18));
			caixaSerie.addKeyListener(new KeyAdapter() {
				public void keyReleased(KeyEvent evt) {
					caixaSerieKeyReleased(evt);
				}
			});
		}
		return caixaSerie;
	}
	
	private JLabel getChecagemTurma() {
		if(checagemTurma == null) {
			checagemTurma = new JLabel();
			checagemTurma.setBounds(332, 196, 72, 16);
			checagemTurma.setFont(new java.awt.Font("Tahoma",0,14));
		}
		return checagemTurma;
	}
	
	private JLabel getJLabel4() {
		if(checagemSerie == null) {
			checagemSerie = new JLabel();
			checagemSerie.setFont(new java.awt.Font("Tahoma",0,14));
			checagemSerie.setBounds(332, 287, 72, 16);
		}
		return checagemSerie;
	}
	
	private JLabel getJLabel4x() {
		if(jLabel4 == null) {
			jLabel4 = new JLabel();
			jLabel4.setText("Obs: Nome da turma eh composto por apenas uma letra.");
			jLabel4.setBounds(66, 224, 365, 21);
			jLabel4.setFont(new java.awt.Font("Tahoma",0,14));
			jLabel4.setForeground(new java.awt.Color(0,102,102));
		}
		return jLabel4;
	}
	
	private JLabel getJLabel5() {
		if(jLabel5 == null) {
			jLabel5 = new JLabel();
			jLabel5.setText("Ex: X E.M. ou E.F.");
			jLabel5.setFont(new java.awt.Font("Tahoma",0,14));
			jLabel5.setForeground(new java.awt.Color(0,102,102));
			jLabel5.setBounds(66, 315, 365, 21);
		}
		return jLabel5;
	}
	
	private void caixaNomeKeyReleased(KeyEvent evt) {
		nomeTurma = caixaNome.getText();
		Pattern padrao = Pattern.compile("[A-Z]{1}");
        Matcher meta = padrao.matcher(nomeTurma);
		if(meta.matches()){
			checagemTurma.setText("OK");
			checagemTurma.setForeground(verde);
			flag = true;
		}else{
			checagemTurma.setForeground(Color.red);
			checagemTurma.setText("INVALIDO");
			flag = false;
		}
	}
	
	private void caixaSerieKeyReleased(KeyEvent evt) {
		nomeSerie = caixaSerie.getText();
		if(Serie.validaNome(nomeSerie)){
			checagemSerie.setText("OK");
			checagemSerie.setForeground(verde);
			flag = true;
		}else{
			checagemSerie.setText("INVALIDO");
			checagemSerie.setForeground(Color.red);
			flag = false;
		}
	}
	
	private JButton getButtonCadastrar() {
		if(buttonCadastrar == null) {
			buttonCadastrar = new JButton();
			buttonCadastrar.setText("CADASTRAR");
			buttonCadastrar.setBounds(324, 510, 112, 25);
			buttonCadastrar.setBackground(new java.awt.Color(255,255,255));
			buttonCadastrar.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent evt) {
					buttonCadastrarMouseClicked(evt);
				}
			});
		}
		return buttonCadastrar;
	}
	
	private JButton getJButton1() {
		if(buttonLimpar == null) {
			buttonLimpar = new JButton();
			buttonLimpar.setText("LIMPAR");
			buttonLimpar.setBackground(new java.awt.Color(255,255,255));
			buttonLimpar.setBounds(480, 510, 112, 25);
			buttonLimpar.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent evt) {
					buttonLimparMouseClicked(evt);
				}
			});
		}
		return buttonLimpar;
	}
	
	private JButton getJButton2() {
		if(buttonCancelar == null) {
			buttonCancelar = new JButton();
			buttonCancelar.setText("CANCELAR");
			buttonCancelar.setBackground(new java.awt.Color(255,255,255));
			buttonCancelar.setBounds(631, 510, 112, 25);
			buttonCancelar.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent evt) {
					buttonCancelarMouseClicked(evt);
				}
			});
		}
		return buttonCancelar;
	}
	
	private void buttonCadastrarMouseClicked(MouseEvent evt) {
		if(flag == false){
			JOptionPane.showMessageDialog(null, "Algum dado invalido");
		}else{
			if(contemSerie(nomeSerie)){
				Turma turma = new Turma(serie,nomeTurma);
				GerenciadorDeJanelas.todasTurmas.add(turma);
				
				String xml = GerenciadorDeJanelas.ARQUIVADOR.toXML(GerenciadorDeJanelas.todasTurmas);
    			GerenciadorDeJanelas.gravar(GerenciadorDeJanelas.arquivoDeTurmas,xml);
    			
    			GerenciadorDeJanelas.window.setContentPane(GerenciadorDeJanelas.menuCadastra);
    	        GerenciadorDeJanelas.window.setVisible(true);
			}else{
				JOptionPane.showMessageDialog(null, "Essa serie nao existe");
			}
		}
	}
	
	private void buttonLimparMouseClicked(MouseEvent evt) {
		limpar();
	}
	
	private void buttonCancelarMouseClicked(MouseEvent evt) {
		limpar();
		GerenciadorDeJanelas.window.setVisible(false);
        GerenciadorDeJanelas.window.setContentPane(GerenciadorDeJanelas.menuCadastra);
        GerenciadorDeJanelas.window.setVisible(true);
	}

	private void limpar(){
    	for(Component component: this.getComponents()){
            if(component instanceof JTextField){
                JTextField caixa = (JTextField)component;
                caixa.setText("");
            }
        }
    	checagemSerie.setText("");
    	checagemTurma.setText("");
    }
	
	private boolean contemSerie(String nomeSerie){
		for(Serie serie2:GerenciadorDeJanelas.todasSeries){
			if(serie2.getNome().equals(nomeSerie)){
				serie = serie2;
				return true;
			}
		}
		return false;
	}
	
}
